package com.huahua.api.controller.entertainment;

import com.fasterxml.jackson.databind.JsonNode;
import com.huahua.api.entity.base.R;
import com.huahua.api.entity.entertainment.website.param.WebsiteGenerateParam;
import com.huahua.api.entity.entertainment.website.pojo.EntertainmentWebsiteGenerateParam;
import com.huahua.api.entity.entertainment.website.pojo.EntertainmentWebsiteGenerateTemplate;
import com.huahua.api.entity.entertainment.website.vo.EntertainmentWebsiteGenerateTemplateVO;
import com.huahua.api.exception.CommonException;
import com.huahua.api.service.entertainment.website.EntertainmentWebsiteGenerateTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author LoveHuaHua
 * @date 2023年08月21日 20:41
 * @description believe in yourself
 */
@RestController
@RequestMapping("/entertainment/websiteGenerate")
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class ConfessionGenerateController {

    private final EntertainmentWebsiteGenerateTemplateService entertainmentWebsiteGenerateTemplateService;


    /**
     * 获取网站参数列表
     *
     * @return {@link JsonNode}
     */
    @PostMapping("/queryParam")
    public Mono<R<JsonNode>> queryParam(@RequestBody WebsiteGenerateParam param) {
        return entertainmentWebsiteGenerateTemplateService.queryParam(param)
                .map(R::success);
    }

    /**
     * 查询模板信息
     *
     * @param param 参数
     * @return {@link EntertainmentWebsiteGenerateTemplateVO}
     */
    @PostMapping("/queryTemplateInfo")
    public Mono<R<EntertainmentWebsiteGenerateTemplateVO>> queryTemplateInfo(@RequestBody EntertainmentWebsiteGenerateTemplate param) {
        return entertainmentWebsiteGenerateTemplateService.queryTemplateInfo(param)
                .map(R::success);
    }

    /**
     * 查询模板列表
     *
     * @param param 参数
     * @return {@link List}
     */
    @PostMapping("/queryTemplateList")
    public Mono<R<List<EntertainmentWebsiteGenerateTemplate>>> queryTemplateList(@RequestBody EntertainmentWebsiteGenerateTemplate param) {
        return entertainmentWebsiteGenerateTemplateService.findByType(param.getType())
                .collectList()
                .map(R::success);
    }

    @PostMapping("/generate")
    public Mono<R<String>> generate(@RequestBody EntertainmentWebsiteGenerateParam param) {
        String templateId = param.getTemplateId();
        return entertainmentWebsiteGenerateTemplateService.findById(Integer.valueOf(templateId))
                .switchIfEmpty(Mono.error(new CommonException("网站模板不存在！请重新选择")))
                .flatMap(template -> entertainmentWebsiteGenerateTemplateService.generate(param)
                        .map(uuid -> template.getBaseUrl() + "?uuid=" + uuid))
                .map(R::success);
    }

}
